#include "page.h"
#include "smqtt.h"
lv_obj_t* scr;
lv_obj_t* scr_body;
lv_obj_t * cont_head;
LV_FONT_DECLARE(myFont);
LV_IMG_DECLARE(blbl22);//哔哩哔哩图片
LV_IMG_DECLARE(blbl33);
LV_IMG_DECLARE(weather1);//天气图片
LV_IMG_DECLARE(airkiss);//微信配网
extern EventGroupHandle_t s_wifi_event_group;//wifi事件组
extern const int CONNECTED_BIT;
extern const int ESPTOUCH_DONE_BIT;
extern const int WIFI_SMART;
extern const int WIFI_CONNET_BIT;
extern const int MQTT_CONNET_BIT;
struct //loge界面元素
{
    lv_obj_t * label_smartconfig ;
    lv_obj_t * bar_sys;
    lv_obj_t * label_rate;
}page_logo;
struct _blbl_sys_t blbl_sys_t;
compment_t c22,c33,cwx;

void blbltv_init()
{
    scr = lv_scr_act();//获取当前活跃的屏幕对象
    static lv_style_t style1;
    lv_style_set_bg_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);//设置屏幕背景
    lv_obj_add_style(scr, LV_BTN_PART_MAIN, &style1);      /*Default button style*/

    scr_body = lv_cont_create(scr, NULL);//屏幕主体
    lv_cont_set_fit2(scr_body,LV_FIT_NONE,LV_FIT_NONE);
    lv_obj_set_size(scr_body,240,135-16);
    lv_obj_set_pos(scr_body,0,16);
    lv_style_set_border_color(&style1, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_obj_add_style(scr_body, LV_BTN_PART_MAIN, &style1);      /*Default button style*/
}
void lv_wifi_init()
{
    cont_head = lv_cont_create(scr, NULL);
    //lv_cont_set_fit(cont_head, LV_FIT_NONE);
    lv_cont_set_fit2(cont_head,LV_FIT_TIGHT,LV_FIT_NONE);
    lv_cont_set_layout(cont_head, LV_LAYOUT_ROW_TOP);
    lv_obj_set_size(cont_head,200,16);

    static lv_style_t style_cont;
    lv_style_set_pad_left(&style_cont,LV_STATE_DEFAULT,0);

    lv_style_set_pad_top(&style_cont,LV_STATE_DEFAULT,0);
    lv_style_set_border_color(&style_cont, LV_STATE_DEFAULT, LV_COLOR_BLACK);
    lv_style_set_bg_color(&style_cont, LV_STATE_DEFAULT, LV_COLOR_BLACK);//设置屏幕背景
    lv_obj_add_style(cont_head, LV_BTN_PART_MAIN, &style_cont);      /*Default button style*/

    //wifi图标
    blbl_sys_t.ico.lv_wifi = lv_label_create(cont_head, NULL);
    lv_label_set_recolor(blbl_sys_t.ico.lv_wifi, true);
    lv_label_set_text(blbl_sys_t.ico.lv_wifi, "#3CB371 " LV_SYMBOL_WIFI);
    lv_obj_set_pos(blbl_sys_t.ico.lv_wifi,0,0);//设置坐标

    //动画
    compment_t chead;
    chead.start=240;
    chead.end=0;
    chead.obj=cont_head;
    chead.start_t=1000;
    chead.end_t=600;
    chead.cb=lv_anim_path_ease_out;
    chead.funcb=(void *)lv_obj_set_x;
    chead.next=NULL;
    obj_move(&chead,1);
    move_free(&chead);

    blbl_sys_t.state.wifi=1;
}
void start_logo()
{
    // img_b22n=calloc(2 * 80*48,sizeof(uint8_t));//22娘 80 0   80
	// img_b33n=calloc(2 * 80*48,sizeof(uint8_t));//23娘 160 0  40
	// /*加载到内存*/
	// PNG_SHOW("/spiffs/2.png", 80, 48,img_b22n,80);
	// PNG_SHOW("/spiffs/3.png", 80, 48,img_b33n,80);
    // //vTaskDelay(10);
    // b22.data=img_b22n;
    // b33.data=img_b33n;

    lv_obj_t * img22 = lv_img_create(scr_body,NULL);
    lv_img_set_src(img22,&blbl22);
    lv_obj_set_pos(img22,-120,40-16);

    lv_obj_t * img33 = lv_img_create(scr_body,NULL);
    lv_img_set_src(img33,&blbl33);
    lv_obj_set_pos(img33,240,40-16);

    //动画

    c22.start=-120;
    c22.end=40;
    c22.obj=img22;
    c22.start_t=1000;
    c22.end_t=600;
    c22.cb=lv_anim_path_ease_out;
    c22.funcb=(void *)lv_obj_set_x;
    c22.next=NULL;


    lv_add_compment(&c22,&c33);
    c33.start=240;
    c33.end=120;
    c33.obj=img33;
    c33.start_t=1000;
    c33.end_t=600;
    c33.cb=(void *)lv_anim_path_ease_out;
    c33.funcb=(void *)lv_obj_set_x;
    c33.next=NULL;

    obj_move(&c22,1);
    ANIEND
    //设置文本
    lv_obj_t * label1 = lv_label_create(scr_body, NULL);
    lv_label_set_recolor(label1, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_align(label1, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_label_set_text(label1, "#0000ff beautiful# #ff00ff words#");
    lv_obj_set_width(label1, 150);
    lv_obj_align(label1, NULL, LV_ALIGN_CENTER, 0, 30);
    obj_move(&c22,0);
    ANIEND
    // free(img_b22n);
    // free(img_b33n);
    //move_free(&c22);

}
void smartconfig_set()
{

    static lv_obj_t * img;
    img = lv_img_create(scr_body,NULL);
    lv_img_set_src(img,&airkiss);
    lv_obj_set_pos(img,240,0);
    cwx.start=240;
    cwx.end=0;
    cwx.obj=img;
    cwx.start_t=600;
    cwx.end_t=600;
    cwx.cb=lv_anim_path_overshoot;
    cwx.funcb=(void *)lv_obj_set_x;
    cwx.next=NULL;
    obj_move(&cwx,1);

    page_logo.label_smartconfig = lv_label_create(scr_body, NULL);
    lv_label_set_long_mode(page_logo.label_smartconfig, LV_LABEL_LONG_BREAK);     /*Break the long lines*/
    lv_label_set_recolor(page_logo.label_smartconfig, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_align(page_logo.label_smartconfig, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_style_local_text_font(page_logo.label_smartconfig,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_text(page_logo.label_smartconfig, "#0000ff 微信扫码或\n关注乐鑫信息科技\n商铺\nairkiss设备#");
    lv_obj_set_width(page_logo.label_smartconfig, 120);
    lv_obj_align(page_logo.label_smartconfig, NULL, LV_ALIGN_CENTER, 60, 0);
    xTaskCreate(smartconfig_example_task, "smartconfig_task", 4096, NULL, 3, NULL);



}
static void event_handler_wifi(lv_obj_t * obj, lv_event_t event)
{

    if(event == LV_EVENT_VALUE_CHANGED) {

        if(lv_msgbox_get_active_btn(obj)==0)
        {
            lv_msgbox_start_auto_close(obj, 0);
            ESP_LOGI("WIFI","start smartconfig");
            smartconfig_set();
            blbl_sys_t.state.wifi=3;
        }
        else
        {
            lv_msgbox_start_auto_close(obj, 0);
            blbl_sys_t.state.wifi=0;
            
        }
    }
}
void switch_wifi()
{
    lv_obj_set_hidden(page_logo.bar_sys,1);
    lv_obj_set_hidden(page_logo.label_rate,1);

    ESP_LOGI("WIFI","start smartconfig");
    smartconfig_set();
    blbl_sys_t.state.wifi=3;
    // static const char * btns[] ={"OK", "NO", ""};
    // lv_obj_t * mbox_wifi;
    // mbox_wifi = lv_msgbox_create(scr, NULL);
    // lv_obj_set_style_local_text_font(mbox_wifi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    // lv_msgbox_set_text(mbox_wifi, "开启微信配网?");
    
    // lv_msgbox_add_btns(mbox_wifi, btns);
    // lv_obj_set_width(mbox_wifi, 200);
    // lv_obj_set_height(mbox_wifi,50);
    // lv_obj_set_event_cb(mbox_wifi, event_handler_wifi);
    // lv_obj_align(mbox_wifi, NULL, LV_ALIGN_CENTER, 0, 0); /*Align to the corner*/

    // blbl_sys_t.group=lv_group_create();
    // lv_group_add_obj(blbl_sys_t.group, mbox_wifi);//加入解码组
    // lv_button_set_group(blbl_sys_t.group);
    // encoder_handler(33);
}
void wifi_rate()
{
    EventBits_t uxBits;

    lv_label_set_text(page_logo.label_rate, "#ff00ff 连接wifi#");
    lv_obj_align(page_logo.label_rate, NULL, LV_ALIGN_CENTER, 0, 30);
    ANIEND
    if(wifi_init()!=ESP_OK)
        switch_wifi();
    while (1) 
    {
        uxBits=xEventGroupGetBits(s_wifi_event_group);
        if(uxBits & CONNECTED_BIT) //配网成功
            break;
        vTaskDelay(pdMS_TO_TICKS(1));
        lv_task_handler();
    }
	if(blbl_sys_t.state.wifi==3)//如果开启微信配网
	{
        uxBits = xEventGroupWaitBits(s_wifi_event_group, WIFI_SMART, true, false, portMAX_DELAY);
        if(uxBits & WIFI_SMART)//等待配网完成
		{
		    cwx.start=0;
            cwx.end=-120;
            obj_move(&cwx,1);
            lv_obj_del(page_logo.label_smartconfig);
            ESP_LOGI("WIFI","wx over");			
		}

	}

    lv_wifi_init();
    ANIEND
}
void fans_rate()
{

    lv_obj_align(page_logo.label_rate, NULL, LV_ALIGN_CENTER, 0, 30);
    get_fans();
    lv_label_set_text(page_logo.label_rate, "#ff00ff 获取粉丝数#");    
}
void time_rate()
{
    lv_obj_set_hidden(page_logo.bar_sys,0);
    lv_obj_set_hidden(page_logo.label_rate,0);

    lv_obj_align(page_logo.label_rate, NULL, LV_ALIGN_CENTER, 0, 30);
    lv_task_handler();
    time_init();
    lv_label_set_text(page_logo.label_rate, "#ff00ff 获取时间#");    
}
void weather_rate()
{

    lv_obj_align(page_logo.label_rate, NULL, LV_ALIGN_CENTER, 0, 30);
    get_weather();
    lv_label_set_text(page_logo.label_rate, "#ff00ff 获取天气#");    
}
void show_rate(void (*fun)(),uint8_t rate)
{
	fun();
    lv_bar_set_value(page_logo.bar_sys, rate, LV_ANIM_ON);
    ANIEND
}
void start_rate()
{
    lv_label_set_text(page_logo.label_rate, "#ff00ff 加载驱动#");
    lv_obj_align(page_logo.label_rate, NULL, LV_ALIGN_CENTER, 0, 30);

}
void dev_init()
{
    show_rate(start_rate,5);
    show_rate(wifi_rate,25);
	show_rate(time_rate,50);
	show_rate(weather_rate,75);    
	show_rate(fans_rate,100);

}
void blbl_sys_init()
{
    lv_obj_clean(scr_body);
    page_logo.bar_sys = lv_bar_create(scr_body, NULL);
    lv_obj_set_size(page_logo.bar_sys, 150, 15);
    lv_obj_align(page_logo.bar_sys, NULL, LV_ALIGN_CENTER, 0, -15);
    lv_bar_set_anim_time(page_logo.bar_sys, 200);

    //设置文本
    page_logo.label_rate = lv_label_create(scr_body, NULL);
    lv_obj_set_style_local_text_font(page_logo.label_rate,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_label_set_recolor(page_logo.label_rate, true);                      /*Enable re-coloring by commands in the text*/
    lv_label_set_align(page_logo.label_rate, LV_LABEL_ALIGN_CENTER);       /*Center aligned lines*/
    lv_obj_set_width(page_logo.label_rate, 150);

    dev_init();
    ANIEND

}
void del_page()
{
    lv_obj_clean(scr_body);//删除scr_body
    move_free(&c22);
}
void lv_message(const char *str,uint16_t t)//系统通知盒
{

    blbl_sys_t.lv_bsgbox=lv_msgbox_create(scr, NULL);//box全局通知
    lv_obj_t * mbox1 =blbl_sys_t.lv_bsgbox;
    lv_obj_set_style_local_text_font(mbox1,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,&myFont);
    lv_msgbox_set_text(mbox1,str);
    lv_msgbox_start_auto_close(mbox1,t);
    lv_obj_align(mbox1, NULL, LV_ALIGN_CENTER, 0, 0); /*Align to the corner*/
}
void blbl_get_temp()
{
    int t,h;
	struct dht11_reading temp;
	char send[30];
    temp=DHT11_read();
	if(!temp.status)
	{
		t=temp.temperature;
		h=temp.humidity;
		if(t>0&&t<70&&h>0&&h<100)
		{
			blbl_sys_t.dht.temperature=t;
			blbl_sys_t.dht.humidity=h;
			sprintf(send,"{\"t\":%d,\"h\":%d}",t,h);
			if(blbl_sys_t.state.mqtt==1&&blbl_sys_t.state.upcloud)
			{
				mqtt_push_message("temp",send);
				//ESP_LOGI("UPCOLUD","上传温湿度");
			}
		}
	}else 
	{
		blbl_sys_t.dht.humidity=0;
		blbl_sys_t.dht.temperature=0;
	}
}
void blbl_task()//总任务，一直在lvgl后台运行
{
    static int t=0,clock=0;
    char str[20];
    get_time(&blbl_sys_t.timeinfo);//获取时间
    if(t==3*60*10)//10分钟分析粉丝数变化
    {
        t=0;
        //ESP_LOGI("BLBL","更新粉丝数");
        uint16_t fensi,guanzhu;
        fensi=get_value_blblfs(1);
        guanzhu=get_value_blblfs(0);
        if(blbl_sys_t.wp.following!=guanzhu)
            blbl_sys_t.wp.following=guanzhu;
        if(blbl_sys_t.wp.follow!=fensi)  
        {
            if(blbl_sys_t.wp.follow>fensi)
            {
                int t=blbl_sys_t.wp.follow-fensi;//粉丝数减少
                sprintf(str,"粉丝-%d",t);
                lv_message((const char *)str,5000);
            }
            else
            {
                int t=fensi-blbl_sys_t.wp.follow;//粉丝数增加
                sprintf(str,"粉丝+%d",t);
                lv_message((const char *)str,5000);
            }
            if(blbl_sys_t.page->index==0)//页面一
            {
                update_fensi();
            }
            blbl_sys_t.wp.follow=fensi;  
        }      

    }
    t++;
    if(blbl_sys_t.state.clock!=0&&(t%10==0))//闹钟总开关打开
     {
        for(uint8_t i=0;i<2;i++)
        {
            if(blbl_sys_t.clock.clock[i].state!=0&&
                blbl_sys_t.timeinfo.tm_hour==blbl_sys_t.clock.clock[i].h
                &&blbl_sys_t.timeinfo.tm_min==blbl_sys_t.clock.clock[i].m)
                {

                         if(clock==0)
                         {
                            //ESP_LOGI("BLBL","闹钟时间到");                             
                             lv_message("叮铃铃",5000);
                         }
                         clock++;
                }else
                {
                    clock=0;
                }
                             
        }

     }
    if(blbl_sys_t.state.mqtt==3&&(t%300==0))//mqtt异常断开
    {
        lv_ico_mqtt_show(3);
    }
}
void blbl_task_temp()
{
    blbl_get_temp();
}
void wifi_state_task()
{
    uint8_t state;
    EventBits_t uxBits;
	uxBits=xEventGroupGetBits(s_wifi_event_group);
    if(uxBits & CONNECTED_BIT)
        state=1;
    else
        state=0;
    if(state!=blbl_sys_t.state.wifi)
    {
        if(state)//连接网络
        {

            ESP_LOGI("SYSTEM","WIFI_CONCONT");
            lv_obj_set_style_local_text_color(blbl_sys_t.ico.lv_wifi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
             lv_label_set_text(blbl_sys_t.ico.lv_wifi,  LV_SYMBOL_WIFI); 
        }else
        {
            ESP_LOGI("SYSTEM","WIFI_DISCONCONT");
            lv_obj_set_style_local_text_color(blbl_sys_t.ico.lv_wifi,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_GRAY);
            lv_label_set_text(blbl_sys_t.ico.lv_wifi,  LV_SYMBOL_WIFI); 
        }
        blbl_sys_t.state.wifi=state;
    }
    if(uxBits & MQTT_CONNET_BIT)
        state=1;
    else
        state=0;  
    if(blbl_sys_t.state.mqtt!=state)
    {
        if(state)
        {
            ESP_LOGI("SYSTEM","MQTT_CONCONT  %d,%d",state,blbl_sys_t.state.mqtt);
            if (blbl_sys_t.ico.lv_mqtt!=NULL)
            {     
           lv_obj_set_style_local_text_color(blbl_sys_t.ico.lv_mqtt,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_YELLOW);
            lv_label_set_text(blbl_sys_t.ico.lv_mqtt,  LV_SYMBOL_UPLOAD);  
            }

        }
        else
        {
            ESP_LOGI("SYSTEM","MQTT_DISCONCONT");
            if (blbl_sys_t.ico.lv_mqtt!=NULL)
            {
            lv_obj_set_style_local_text_color(blbl_sys_t.ico.lv_mqtt,LV_LABEL_PART_MAIN,LV_STATE_DEFAULT,LV_COLOR_GRAY);
            lv_label_set_text(blbl_sys_t.ico.lv_mqtt,  LV_SYMBOL_UPLOAD);  
            }

        }
        blbl_sys_t.state.mqtt=state;
    }

}
void test_start()
{
    blbltv_init();//lvgl初始化
    start_logo();//开机logo
    blbl_sys_init();//驱动初始化
    del_page();//删除当前页
    blbl_sys_info_init();//系统信息初始化
    get_time(&blbl_sys_t.timeinfo);//获取时间
    lv_task_create(blbl_task, 100, LV_TASK_PRIO_MID, NULL);//总任务 负责时间和通知
    lv_task_create(blbl_task_temp, 4000, LV_TASK_PRIO_HIGH, NULL);//      温湿度任务  
    lv_task_create(wifi_state_task, 10000, LV_TASK_PRIO_LOW, NULL);//      wifi检测任务  
    ESP_LOGI("SYSTEM","esp_get_free_heap_size : %d ", esp_get_free_heap_size());
    page1_start();//开启页面一
}
